﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BrightIdeasSoftware;
using GzipManagerPro.UI.Controls;
using GzipManagerPro.UI.Controls.Preview;
using GzipManagerPro.UI.Dialogs;
using GzipManagerPro.UI.Utility;

namespace GzipManagerPro.UI
{
    public partial class MainForm : Form
    {
        private string CommandLinePath;

        public MainForm(): this(null)
        {
        }

        public MainForm(string fileName)
        {
            InitializeComponent();

            
            this.CommandLinePath = fileName;


        }

        private void CommandViewArchive(string fileName)
        {
            ShowArchive(new CustomFileSystemInfo(new FileInfo(fileName)));
            this.MainSplitContainer.Panel1Collapsed = true;
            this.View.Visible = false;
        }

        
        private void ShowArchive(CustomFileSystemInfo info)
        {
            this.ClearToolStripItems();
            this.ClearFileMenuItems();

            

            GzipManagerPro.UI.Controls.Preview.PreviewBase control = GzipManagerPro.UI.Controls.Preview.Factory.Create(info.FullName);
            control.Dock = DockStyle.Fill;
            this.MainSplitContainer.Panel2.Controls.Add(control);

            ICommandProvider commandProvider = control as ICommandProvider;
            if (commandProvider == null)
            {
                this.ToolStrip.Visible = false;
                return;
            }


            Command[] commands = commandProvider.GetCommands(info);
            int index = 0;
            foreach (var command in commands)
            {
                //if (command.Method == null)
                //    throw new InvalidOperationException();

                this.ToolStrip.Items.Add(CreateButtonFromCommand(command));
                this.File.DropDownItems.Insert(index++, CreateMenuFromCommand(command));
            }

            this.ToolStrip.Visible = this.ToolStrip.Items.Count > 0;
            this.File.DropDownItems.Insert(index++, new ToolStripSeparator());
        }

        private void FileExplorer_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.FileExplorer.SelectedItem == null)
            {
                this.MainSplitContainer.Panel2.Controls.Clear();
                return;
            }

            CustomFileSystemInfo info = (CustomFileSystemInfo)this.FileExplorer.SelectedItem.RowObject;
            this.ShowArchive(info);

            
        }

        private ToolStripItem CreateButtonFromCommand(Command command)
        {
            if (command is CommandSeparator)
                return new ToolStripSeparator();

            ToolStripButton button = new ToolStripButton()
            {
                Text = command.Title,
                Image = command.Image,
                Tag = command
            };

            if (command.Image == null)
                button.DisplayStyle = ToolStripItemDisplayStyle.Text;
            else if (string.IsNullOrEmpty(command.Title) && command.Image != null)
                button.DisplayStyle = ToolStripItemDisplayStyle.Image;
            else if (!string.IsNullOrEmpty(command.Title) && command.Image != null)
                button.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
            else
                throw new Exception("Invalid command!");

            button.Click += CommandItem_Click;

            return button;
        }

        private ToolStripItem CreateMenuFromCommand(Command command)
        {
            if (command is CommandSeparator)
                return new ToolStripSeparator();
            
            ToolStripMenuItem menu = new ToolStripMenuItem()
            {
                Text = command.Title,
                Image = command.Image,
                Tag = command
            };

            menu.Click += CommandItem_Click;

            return menu;
        }

        private void CommandItem_Click(object sender, EventArgs e)
        {
            try
            {
                CustomFileSystemInfo info = (CustomFileSystemInfo)this.FileExplorer.SelectedItem.RowObject;
                Command c = (Command)(sender as ToolStripItem).Tag;
                if(c.Method(info))
                    this.FileExplorer.RebuildAll(true);
            }
            catch
            {
            }
        }

        private void ClearFileMenuItems()
        {
            while (this.File.DropDownItems.Count > 1)
                this.File.DropDownItems.RemoveAt(0);
        }

        private void ClearToolStripItems()
        {
            this.ToolStrip.Items.Clear();
        }

        private void FileExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ViewPreview_Click(object sender, EventArgs e)
        {
            this.MainSplitContainer.Panel2Collapsed = !this.ViewPreview.Checked;
        }

        private void HelpAbout_Click(object sender, EventArgs e)
        {
            new AboutDialog().ShowDialog();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            if (this.CommandLinePath == null)
                return;

            CommandViewArchive(this.CommandLinePath);
            
        }
    }
}
